// pages/order/order.js
const app = getApp()
import http from "../../utils/http"
import util from "../../utils/util"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    unread:2,
    tabList:['接单池','待派单','待接单','进行中/待核销','已完成','售后订单'],
    orderState:1,
    list:[],
    finish: false,
    loading: false,
    page: 1,
    active: 0,
    token: wx.getStorageSync('token')
  },
  notice(){
    
  },
  orderBtnTap(e){
    if(e.Detail.type == 1){
      wx.requestSubscribeMessage({
        tmplIds: [app.globalData.templateconfig.shop_order_template,app.globalData.templateconfig.shop_finish_template,app.globalData.templateconfig.shop_sales_template],
        complete :(res) =>{ 
          http.get('order/shopaccept', {
            id: e.Detail.info.id
          }, true, false).then(res => {
            this.setData({
              active: 1
            })
            this.reload()
            util.toast('抢单成功，请派单')
          }).catch(e => {
            let data = e.data
            if(data.code === 2){
              util.toast(data.msg)
              setTimeout(()=>{
                util.skip('/service/earnestMoney/earnestMoney')
              },1000)
            }else if(data.code === 0){
              util.toast(data.msg)
            }
          })
        }
      })
      
    }else if(e.Detail.type == 2){
      util.skip('/pages/allocation/allocation?id='+e.Detail.info.id)
    }else if(e.Detail.type == 3){
      wx.openLocation({
        latitude : Number(e.Detail.info.address.lat),
        longitude : Number(e.Detail.info.address.lng),
        name: e.Detail.info.address.area,
        scale: 18
      })
    }
  },
  orderTap(e){
    util.skip('/pages/orderDetail/orderDetail?order_id='+e.Detail.id)
  },
  reload(){
    this.setData({
      list: [],
      page: 1,
      finish: false
    })
    this.getList()
  },
  more(){
    this.setData({
      page: ++this.data.page
    })
    this.getList()
  },
  getList(){
    if(!wx.getStorageSync('token')) return
    if(this.data.finish){
      return 
    }
    if(this.data.loading){
      return 
    }
    this.setData({
      loading: true
    })
    let data = {
      page: this.data.page,
    }
    if(this.data.active === 0){
      data['is_pool'] = 1
    }else if(this.data.active === 1){
      data['not_allocation'] = 1
    }else if(this.data.active === 2){
      data['not_accept'] = 1
    }else if(this.data.active === 3){
      data['not_finish'] = 1
    }else if(this.data.active === 4){
      data['finish'] = 1
    }else if(this.data.active === 5){
      data['in_service'] = 1
    }
    http.get('order/shoporderlist', data).then(res => {
      if(res.data.length === 0){
        this.setData({
          finish: true
        })
      }
      let arr = this.data.list.concat(res.data)
      this.setData({
        list: arr
      })
      
    }).finally(res => {
      this.setData({
        loading: false
      })
    })
  },
  //派单
  toallocation(){
    wx.navigateTo({
      url: '/pages/allocation/allocation',
    })
  },
  toDetail(){
    wx.navigateTo({
      url: '../orderDetail/orderDetail',
    })
  },
  onChange(event) {
    this.setData({
      active: event.Detail.index
    })
    this.reload()
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    
  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      token: wx.getStorageSync('token')
    })
    this.reload()
  },
})